package com.jsg.sl651.entity.frame.body.returns;

import com.jsg.sl651.util.ByteUtil;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DefaultReturnResponse extends ReturnMessageBody {
    public DefaultReturnResponse(int flowNumber) {
        this.flowNumber = flowNumber;
        this.sendTime = LocalDateTime.now();
    }

    @Override
    protected byte[] encodeHex() throws Exception {
        byte[] bytes = new byte[8];
        bytes[0] = (byte) ((flowNumber >>> 8) & 0XFF);
        bytes[1] = (byte) (flowNumber & 0XFF);
        String dateStr = sendTime.format(DateTimeFormatter.ofPattern(LOCAL_DATE_TIME_PATTERN)).substring(2);
        for (int i = 0; i < 6; i++) {
            bytes[i + 2] = ByteUtil.toByte(dateStr.substring(i * 2, i * 2 + 2));
        }
        return bytes;
    }

    @Override
    protected byte[] encodeAscii() throws Exception {
        byte[] bytes = new byte[16];
        // 长度为4
        String hex = ByteUtil.toHex((byte) ((flowNumber >>> 8) & 0XFF)) + ByteUtil.toHex((byte) (flowNumber & 0XFF));
        // 时间长度为12
        String dateStr = sendTime.format(DateTimeFormatter.ofPattern(LOCAL_DATE_TIME_PATTERN)).substring(2);
        hex = hex + dateStr;
        for (int i = 0; i < hex.length(); i++) {
            bytes[i] = (byte) hex.charAt(i);
        }
        return bytes;
    }
}
